"""
@作者: sipeiliu@163.com
@功能: 服务端的GUI界面
"""
import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMainWindow, QDockWidget, QApplication, QLineEdit,\
        QPushButton, QTableWidget, QTableWidgetItem, QMessageBox
import re

import send

class MainWindow(QMainWindow):
    def __init__(self, ref_ip):
        super().__init__()
        # 更新字典 {车辆编号：树莓派IP} 
        self.ref_ip = ref_ip
        '''主窗口'''
        self.setWindowTitle("fs") # 主窗口名
        # 字典表格
        self.table_dict = QTableWidget()
        self.table_dict.setRowCount(len(self.ref_ip)) # 行数
        # self.table_dict.setRowHeight(0, 20) # 行高
        self.table_dict.setColumnCount(2) # 列数
        self.table_dict.setColumnWidth(1, 200) # 第二列宽
        self.table_dict.setHorizontalHeaderLabels(["车辆编号", "车辆IP"])
        self.setCentralWidget(self.table_dict)

        '''添加'''
        # 添加文本框 车辆编号
        self.text_add_ref = QLineEdit(self)
        self.dock_add_text_ref = QDockWidget("车辆编号", self)
        self.dock_add_text_ref.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.RightDockWidgetArea , self.dock_add_text_ref)
        self.dock_add_text_ref.setWidget(self.text_add_ref)
        # 添加文本框 车辆IP
        self.text_add_ip = QLineEdit(self)
        self.dock_add_text_ip = QDockWidget("车辆IP", self)
        self.dock_add_text_ip.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_add_text_ip)
        self.dock_add_text_ip.setWidget(self.text_add_ip)
        # 添加按钮
        self.button_add = QPushButton("添加")
        self.dock_add_button = QDockWidget("", self)
        self.dock_add_button.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock_add_button)
        self.dock_add_button.setWidget(self.button_add)
        self.button_add.clicked.connect(self.add_car)

        '''删除'''
        # 删除文本框 车辆编号
        self.text_delete_ref = QLineEdit(self)
        self.dock_delete_text_ref = QDockWidget("车辆编号", self)
        self.dock_delete_text_ref.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_delete_text_ref)
        self.dock_delete_text_ref.setWidget(self.text_delete_ref)
        # # 删除文本框 车辆IP
        # self.text_delete_ip = QLineEdit(self)
        # self.dock_delete_text_ip = QDockWidget("车辆IP", self)
        # self.dock_delete_text_ip.setFeatures(QDockWidget.NoDockWidgetFeatures) 
        # self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_delete_text_ip)
        # self.dock_delete_text_ip.setWidget(self.text_delete_ip)
        # 删除按钮
        self.button_delete = QPushButton("删除")
        self.dock_delete_button = QDockWidget("", self)
        self.dock_delete_button.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_delete_button)
        self.dock_delete_button.setWidget(self.button_delete)
        self.button_delete.clicked.connect(self.delete_car)

        '''发送'''
        # 发送按钮
        self.button_add = QPushButton("发送")
        self.dock_add_button = QDockWidget("", self)
        self.dock_add_button.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(Qt.BottomDockWidgetArea , self.dock_add_button)
        self.dock_add_button.setWidget(self.button_add)
        self.button_add.clicked.connect(self.send_code)

        # 刷新表格
        self.refresh_table()
    
    def add_car(self):
        # 读取文本框
        ref = self.text_add_ref.text()
        ip = self.text_add_ip.text()
        # TODO：车辆标号方式未知，暂时无法判断格式
        pass
        # 正则表达式检测ip输入是否正确
        re_ip = r"(^((2[0-4]\d.)|(25[0-5].)|(1\d{2}.)|(\d{1,2}.))((2[0-5]{2}.)|(1\d{2}.)|(\d{1,2}.){2})((1\d{2})|(2[0-5]{2})|(\d{1,2})))"
        if re.search(re_ip, ip) != None:
            # 添加到字典
            self.ref_ip.update({ref:ip})
            # 刷新表格
            self.refresh_table()
        else:
            QMessageBox.critical(self, "错误","请检查输入")

    def delete_car(self):
        # 读取文本框
        ref = self.text_delete_ref.text()
        ip = self.text_delete_ref.text()
        # 通过编号检测字典中是否有该车
        if ref in self.ref_ip:
            self.ref_ip.pop(ref)
            # 刷新表格
            self.refresh_table()
        else:
            QMessageBox.critical(self, "错误","请检查输入")


    def refresh_table(self):
        # 刷新行数
        self.table_dict.setRowCount(len(self.ref_ip)) # 行数
        # 刷新数据
        for index,key in enumerate(self.ref_ip):
            # 对ref、ip创建QTableWidgetItem对象
            ip_Item = QTableWidgetItem(self.ref_ip[key])
            ref_Item = QTableWidgetItem(key)
            # 写入单元格
            self.table_dict.setItem(index, 0, ref_Item)
            self.table_dict.setItem(index, 1, ip_Item)
        
        # 刷新表格内容
        self.table_dict.viewport().update()

    def send_code(self):
        # 实例化服务端对象
        server = send.ServerSocket(self.ref_ip)
        # 压缩项目文件
        server.zip_file()
        # 发送项目文件
        server.send_file()

def run(args=None):
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

if __name__ == '__main__':
    run()